package com.wyg.service;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.wyg.dao.UserDao;
import com.wyg.pojo.User;

import javafx.scene.control.Alert;

public class LoginService {

	public HttpServletRequest request;
	public HttpServletResponse response;
	public UserDao ud = new UserDao();
	
	public LoginService(HttpServletRequest request, HttpServletResponse response) {
		super();
		this.request = request;
		this.response = response;
	}
	
	public void login(){
		String phone = request.getParameter("phone");
		String password = request.getParameter("password");
		
		User user = ud.login(phone, password);
		if(user!=null){
			request.getSession().setAttribute("user", user);
			try {
				if(phone.equals("admin")){
					request.getRequestDispatcher("main/index.jsp").forward(request, response);
				}else{
					request.getRequestDispatcher("usermain/index.jsp").forward(request, response);
				}
			} catch (ServletException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}else{
			try {
				response.getWriter().print("<script>alert(\"账号或密码错误\")</script>");
				response.sendRedirect("view/login.html");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
}
